<?php
error_reporting(E_ALL);
class MochiFeed {
/******************************************************************************/
private function getXMLValue($xml, $xpath) {
    if ($node = $xml->xpath($xpath)) {
         $value = (string) $node[0];
    } else {
        $value = '';
    }

    return $value;
}
/******************************************************************************/
private function getGame($uuid) {
    $sql = "select * from " . DBTABLE . " where uuid='$uuid'";
    $result = mysql_query($sql) or die("$sql\n" . mysql_error());
    $rec = mysql_fetch_assoc($result);

    return $rec;
}
/******************************************************************************/
private function saveGame($rec, $oldrec) {
   
    $fields = '';
    foreach($rec as $field => $value) {
        if ($fields != '') {
            $fields .= ', ';
        }
        $fields .= "`$field` = '" . mysql_real_escape_string($value) . "'";
    }

    if ($oldrec) {
        // update record
        $sql = "update " . DBTABLE . " set $fields where uuid='" . $rec['uuid'] . "'";
    } else {
        // insert record;
        $sql = "insert into " . DBTABLE . " set $fields";
    }

    mysql_query($sql) or die("$sql\n" . mysql_error());

}
/******************************************************************************/
private function getZip($zip_url, $rec, $oldrec) {
  
    $gamedir = GAMEDIR;
    $gamezip = substr($zip_url, 28);
    echo "$zip_url => $gamedir/$gamezip\n";
   
    if (!isset($oldrec['time'])) {
        $oldrec['time'] = '';
    }

   
    if ((strtotime($rec['time']) != strtotime($oldrec['time'])) || (!file_exists("$gamedir" . $rec['swfurl']))) {
        echo "zip: " . strtotime($rec['time']) . ' != ' . strtotime($oldrec['time']) . " $zip_url $gamedir" . $rec['swfurl'] . "\n";
        echo $rec['time'] . "!=" . $oldrec['time']; 
    
        $cmd = 'curl "' . $zip_url .'" -o "' . "$gamedir/$gamezip"  . '"';
        echo $cmd;
        system($cmd);

        
        $cmd = "unzip -o $gamedir/$gamezip -d $gamedir";
        echo "$cmd\n";
        system($cmd); 

        unlink("$gamedir/$gamezip");
    } 
}
/******************************************************************************/
public function getFeed($feed = 'mochi.xml') {
    echo "read data\n";
    $reader = new XMLReader();
    $reader->open($feed);

    while ($reader->read()) {
        if ($reader->nodeType == XMLREADER::ELEMENT && $reader->name == 'entry') {
            $node = $reader->expand();
            $dom = new DomDocument();
            $n = $dom->importNode($node, true);
            $dom->appendChild($n);

            $xml = simplexml_import_dom($n);
            //echo $xml->asXMl();
            $namespaces = $xml->getNamespaces(true);

            // set record
            $rec = array();
            $rec['nameid'] = $this->getXMLValue($xml, '//dt[@class="slug"]');

            $media = $xml->children($namespaces['media']);
            // map data
            $rec = array();
            $rec['name'] = (string) $media->title;
            $rec['desc'] = (string) $media->description;
            $rec['time'] = str_replace('Z', '', (string) $xml->published);
            $rec['width'] = (string) $media->player->attributes()->width; 
            $rec['height'] = (string) $media->player->attributes()->height;
        
            $rec['cat'] = '';
            foreach ($xml->category as $category) {
                if ($rec['cat'] != '') {
                    $rec['cat'] .= ', ';
                }
                $rec['cat'] .= $category->attributes()->term; 
            }

            $rec['authorsite'] = (string) $xml->author->uri;
            $rec['authorname'] = (string) $xml->author->name;
            $rec['keywords'] = (string) $media->keywords; 
            $rec['uuid'] = (string) $xml->id;        
            $rec['thumb'] = (string) $media->thumbnail->attributes()->url;
            $rec['swfurl'] = '/swf' . substr((string) $media->player->attributes()->url, 29);

            $xml->registerXPathNamespace('default', $namespaces['default']);
            $rec['instructions'] = $this->getXMLValue($xml, '//default:dd[@class="instructions"]');
            $rec['nameid'] = $this->getXMLValue($xml, '//default:dd[@class="slug"]');
            $rec['gameurl'] =  '/games/' . $rec['nameid'];
       
             //print_r($rec);
            $oldrec = $this->getGame($rec['uuid']);
            $this->saveGame($rec, $oldrec);
            $zip_url = $this->getXMLValue($xml, '//default:dd[@class="zip_url"]');
            
               
            $this->getZip($zip_url, $rec, $oldrec);
        }
    }
    $reader->close();
}
/******************************************************************************/
// end class
}
?>
